package mail

import (
	"bytes"
	"io"
	"os"
	"io/ioutil"
)

type MyReader struct {
	data *bytes.Buffer
	last byte
}

func NewMyReader( buffer []byte) (*MyReader) {
	return &MyReader{ bytes.NewBuffer(buffer), 0}
}
func NewMyReader2( in io.Reader) (*MyReader) {
	buffer, _ := ioutil.ReadAll( in)
	return &MyReader{ bytes.NewBuffer(buffer), 0}
}

func (r *MyReader) Read(p []byte) (n int, err os.Error) {
 	c, err := r.data.ReadByte()
	if err != nil {
//logger.Printf ("err:%v", err) 	
		return 0, err
	}
	if c == '\n' && r.last != '\r' {
		p[0] = '\r'
		p[1] = c
		r.last = c
//logger.Printf ("leido:\r\n", c) 	
		return 2, nil
	}
	r.last = c
 	p[0] = c
//logger.Printf ("leido:%c %02x", c, c) 	
	return 1, nil
}
